package com.iad.controller.command
{
  import com.adobe.cairngorm.commands.Command;
  import com.adobe.cairngorm.control.CairngormEvent;
  import com.iad.controller.event.*;
  import com.iad.controller.service.*;
  import com.iad.model.*;
  import com.iad.model.valueobject.*;
  import com.iad.view.component.*;
  import com.iad.view.form.*;
  import com.iad.view.util.ascb.util.*;
  
  import mx.collections.ArrayCollection;
  import mx.managers.CursorManager;
  
  public class PageCommand implements Command
  {
    [Bindable]
    private var model:ModelLocatorImpl = ModelLocatorImpl.getInstance();
    
 
    public function execute(event:CairngormEvent):void {
      switch (event.type) {
        case PageEvent.GET_PAGES:
          getPages((event as PageEvent).object as int);
        break;
        case PageEvent.SAVE_PAGE:
          savePage((event as PageEvent).object as Page);
        break;
        case PageEvent.SAVE_PAGES:
          savePages();
        break;
     }
    }
    
    private function savePage(page:Page):void {
      var service:SiteService = new SiteService();
      service.addEventListener(ServiceResultEvent.SAVE_PAGE_RESULT, savePageOnResult, false, 0, false);
      service.addEventListener(ServiceErrorEvent.ERROR, onError, false, 0, true);
      service.savePage(page);
    }
    
     
    protected function savePageOnResult(event:ServiceResultEvent):void {
      trace("PageCommand::savePageOnResult()");
      model.signupWizard.selectedChild = model.signupWizard.step3;
      model.signupWizard.step2.enabled = false;
      model.signupWizard.step3.enabled = true;
    }
    
    private function savePages():void {
      var service:SiteService = new SiteService();
      service.addEventListener(ServiceResultEvent.SAVE_PAGES_RESULT, savePagesOnResult, false, 0, false);
      service.addEventListener(ServiceErrorEvent.ERROR, onError, false, 0, true);
      service.savePages();
    }
    
     
    protected function savePagesOnResult(event:ServiceResultEvent):void {
      trace("PageCommand::savePagesOnResult()");
    }
    
    private function getPages(siteId:int):void {
      CursorManager.setBusyCursor();
      var service:SiteService = new SiteService();
      if (siteId != 0) {
        service.addEventListener(ServiceResultEvent.GET_SITE_PAGES_RESULT, getSitePagesOnResult, false, 0, false);
      }
      else {
        service.addEventListener(ServiceResultEvent.GET_MAIN_PAGES_RESULT, getMainPagesOnResult, false, 0, false);
      }
      service.addEventListener(ServiceErrorEvent.ERROR, onError, false, 0, true);
      service.getPages(siteId);
    }
    
     
    private function onError(event:ServiceResultEvent):void {
      CursorManager.removeBusyCursor();
      trace("error: " + event);
    }
    
    protected function getMainPagesOnResult(event:ServiceResultEvent):void {
      trace("PageCommand::getMainPagesOnResult()");
      CursorManager.removeBusyCursor();
      var pages:ArrayCollection = event.result as ArrayCollection;
      model.mainPages = pages;
      model.app.mainView.loadPages();
    }
    
    protected function getSitePagesOnResult(event:ServiceResultEvent):void {
      trace("PageCommand::getSitePagesOnResult()");
      CursorManager.removeBusyCursor();
      var pages:ArrayCollection = event.result as ArrayCollection;
      model.sitePages = pages;
      if (model.app.currentState == model.ARTIST_VIEW) {
        model.app.artistView.loadPages();
      }
    }
     
  } 
}
    